package com.minshenglife.zanzan.web.power;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.minshenglife.commons.dal.Page;
import com.minshenglife.zanzan.entity.Dept;
import com.minshenglife.zanzan.entity.User;
import com.minshenglife.zanzan.entity.power.ManagerEmp;
import com.minshenglife.zanzan.entity.power.TempDept;
import com.minshenglife.zanzan.entity.power.UserDeptRole;
import com.minshenglife.zanzan.service.power.ManagerEmpService;
import com.minshenglife.zanzan.service.power.TempDeptService;
import com.minshenglife.zanzan.service.power.UserPowerService;
import com.minshenglife.zanzan.service.power.UserRoleService;
import com.minshenglife.zanzan.web.power.userpower.UserPowerPage;

/**
 * 人员角色管理/职位
 * 
 * @author wangpeng
 * 
 */
@Controller
@RequestMapping("/userRole")
public class UserRoleController {
	Logger log = LoggerFactory.getLogger(UserPowerController.class);

	@Resource
	private UserRoleService userRoleService;

	@Resource
	private UserPowerService userPowerService;

	@Resource
	ManagerEmpService managerEmpService;

	@Resource
	TempDeptService tempDeptService;

	/**
	 * 分页获取用户信息
	 * 
	 * @param map
	 * @param pp
	 * @return
	 */
	@RequestMapping(value = "/loadUserRole.power", method = RequestMethod.GET)
	public String lodeUserRole(HttpServletRequest request, ModelMap modleMap, UserPowerPage pp) {
		log.info("start    lodeUserRole()");
		int pageNo = pp.getPageNo() == 0 ? 1 : pp.getPageNo();
		int pageSize = pp.getPageSize() == 0 ? 10 : pp.getPageSize();
		HttpSession session = request.getSession();
		ManagerEmp managerEmp = (ManagerEmp) session.getAttribute("managerEmp");
		if (managerEmp.getGrade().equals("1")) {
			modleMap.put("dept_name", "");
		} else {
			String dept_name = managerEmpService.finddet_name(managerEmp.getDept_id());
			modleMap.put("dept_name", dept_name);
		}
		Page<UserDeptRole> page = userRoleService.findUserdept(modleMap, pageNo, pageSize);
		if (managerEmp.getGrade().equals("1")) {
			modleMap.addAttribute("add", true);
			/*
			 * for (int i = 0; i < page.getRows().size(); i++) { System.out.println(page.getRows().get(i).getMpEmpId()); }
			 */
		} else {
			modleMap.addAttribute("add", false);
		}
		modleMap.addAttribute("page", page);
		return "/jsp/power/userRole";
	}

	/**
	 * 根据用户名搜索用户
	 * 
	 * @param map
	 * @param userName
	 * @return
	 */
	@RequestMapping(value = "/findUserName.power", method = RequestMethod.GET)
	public String findUserName(HttpServletRequest request, ModelMap modleMap, String userName) {
		log.info("start    findUserName()");
		HttpSession session = request.getSession();
		ManagerEmp managerEmp = (ManagerEmp) session.getAttribute("managerEmp");
		if (managerEmp.getGrade().equals("1")) {
			modleMap.put("dept_name", "");
		} else {
			String dept_name = managerEmpService.finddet_name(managerEmp.getDept_id());
			modleMap.put("dept_name", dept_name);
		}
		Page<UserDeptRole> page = userRoleService.findUserName(modleMap, userName);
		if (managerEmp.getGrade().equals("1")) {
			modleMap.addAttribute("add", true);
		} else {
			modleMap.addAttribute("add", false);
		}
		modleMap.put("page", page);
		return "/jsp/power/userRole";
	}

	/**
	 * 根据empId获取个人信息
	 * 
	 * @param map
	 * @param empId
	 * @return
	 */
	@RequestMapping(value = "/updateUserRole.power", method = RequestMethod.GET)
	public String updateUserRole(ModelMap map, String empId) {
		log.info("start    updateUserRole()");
		UserDeptRole userDeptRole = userRoleService.findUserDeptRole(map, empId);
		map.addAttribute("userDeptRole", userDeptRole);
		return "/jsp/power/updateUserRole";
	}

	/**
	 * 获取分公司
	 * 
	 * @param request
	 * @param dept_id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/lodedeptSheng.power", method = RequestMethod.GET)
	public String lodedeptSheng(HttpServletRequest request, String dept_id) {
		log.info("start    lodedeptSheng()");
		String data;
		if (!dept_id.equals("null")) {
			data = userRoleService.findDept(dept_id);
		} else {
			HttpSession session = request.getSession();
			ManagerEmp managerEmp = (ManagerEmp) session.getAttribute("managerEmp");
			dept_id = managerEmp.getDept_id();
			if (dept_id.equals("1")) {
				data = userRoleService.findDept(dept_id);
			} else {
				data = userRoleService.findfFiliale(dept_id);
			}
		}
		return data;
	}

	/**
	 * 保存更改后的部门信息
	 * 
	 * @param fengongsi
	 * @param bumen
	 * @param jigou
	 * @param zhiwei
	 * @return
	 */
	@RequestMapping(value = "/updateDept.power", method = RequestMethod.GET)
	public String updateDept(String empId, String fengongsi, String bumen, String jigou, String zhiwei, ModelMap modelMap, HttpServletRequest request) {
		log.info("start    updateDept()");
		String dept_id = null;
		if (zhiwei != null) {
			dept_id = zhiwei;
		} else if (jigou != null) {
			dept_id = jigou;
		} else if (bumen != null) {
			dept_id = bumen;
		} else if (fengongsi != null) {
			dept_id = fengongsi;
		} else {

		}
		TempDept td = null;
		td = tempDeptService.findTD(empId);
		if (null != td && td.getEmpId() != null) {
			td.setDept_id(dept_id);
			tempDeptService.updateTempDeptDept_id(td);
		} else {
			td = new TempDept();
			td.setEmpId(empId);
			td.setDept_id(dept_id);
			tempDeptService.addTempDeptDept_id(td);
		}
		return "/jsp/power/results";
	}

	/**
	 * 跳转到添加管理员
	 * 
	 * @param map
	 * @param empId
	 * @return
	 */
	@RequestMapping(value = "/addManagerEmp.power", method = RequestMethod.GET)
	public String addManagerEmp(ModelMap map, String empId) {
		log.info("start    addManagerEmp()");
		User user = userPowerService.findUserId(empId);
		Dept dept = userRoleService.getDept(user.getDept_id());
		map.addAttribute("dept_name",dept.getDept_name());
		map.addAttribute("user", user);
		return "/jsp/power/addManager";
	}
}
